package com.xcw.jac.deoc.threadprctice;

/**
 * @author xcw
 * @date 2023/8/17 16:13
 * @description
 */
public class F2ThreadPrac {

    public static final int MAX_NUMBER = 100;
    public static int start_number = 1;
    public static final Object lock = new Object();
    /**
     * 两个线程，一个打印奇数，一个打印偶数，然后顺序打印出1-100
     */

    public static void main(String[] args) {
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                while (start_number <= MAX_NUMBER) {
                    System.out.println(Thread.currentThread().getName() + " [--] "  + "当前的值 " + start_number);
                    synchronized (lock) {
                        System.out.println((start_number % 2));
                        if (start_number % 2 == 0) {

                            try {
                                lock.wait();
                                System.out.println(Thread.currentThread().getName() + " [--] " + "等待锁*******");
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        } else{

                            System.out.println(Thread.currentThread().getName() + " [--] " + start_number);
                            start_number++;
                            lock.notify();
                            System.out.println(Thread.currentThread().getName() + " [--] " + "释放锁*******");
                    }
                }
                }
            }
        };
        Thread thread1 = new Thread(runnable1);
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                while (start_number <= MAX_NUMBER) {
                    synchronized (lock) {
                        System.out.println(Thread.currentThread().getName() + " [--] " + "当前的值 " + start_number);
                        if (start_number % 2 == 1) {
                            try {
                                lock.wait();
                                System.out.println(Thread.currentThread().getName() + " [--] " + "等待锁*******");
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            System.out.println(Thread.currentThread().getName() + " [--] " + start_number);
                            start_number++;
                            lock.notify();
                            System.out.println(Thread.currentThread().getName() + " [--] " + "释放锁*******");
                        System.currentTimeMillis();
                        System.currentTimeMillis();

                        }
                    }
                }
            }
        };
        Thread thread2 = new Thread(runnable2);
        thread1.start();
        thread2.start();

    }
}
